package com.example.musictest.ui.dashboard;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.musictest.R;
import com.example.musictest.databinding.FragmentDashboardBinding;
import com.example.musictest.ui.adapter.VideoAdapter;
import com.example.musictest.ui.home.HomeViewModel;
import com.fw.mediasession.media.bean.MyVideo;
import com.zy.arouter.ARouterConstants;
import com.zy.base.fragment.BaseFragment;
import com.zy.base.recycleview.BaseRecycleAdapter;
import com.zy.untils.MyLog;

import java.util.ArrayList;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class DashboardFragment extends BaseFragment {

    private DashboardViewModel dashboardViewModel;
    private FragmentDashboardBinding binding;
    private HomeViewModel homeViewModel;
    private VideoAdapter adapter;
    private ArrayList<MyVideo> myVideos;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel =
                new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(DashboardViewModel.class);

        binding= DataBindingUtil.inflate(inflater, R.layout.fragment_dashboard, container, false);

        View root = binding.getRoot();


        homeViewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class);
        initView();
        return root;
    }
    private FragmentActivity context;
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context=requireActivity();
    }
    private void initView() {
        adapter = new VideoAdapter(requireActivity(),new ArrayList<>(), R.layout.item_video_recy);
        LinearLayoutManager layoutManager = new LinearLayoutManager(requireActivity());
        binding.videoRecyclerview.setLayoutManager(layoutManager);
        binding.videoRecyclerview.setAdapter(adapter);

        homeViewModel.videos.observe(context, videos -> {
            MyLog.i("数据：videos"+videos.size()) ;
            myVideos=videos;
            adapter.updateData(myVideos);
        });
        adapter.setOnItemClickListener(new BaseRecycleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                MyLog.i("点击：music"+position) ;
                homeViewModel.playMediaList(myVideos,position,1);
                Navigation.findNavController(view).navigate(R.id.action_navigation_dashboard_to_navigation_video);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}